//*********************************************************************
//		Copyright (c) 2003 - 2008 Microsoft Corporation.
//      This code is provided under Microsoft Public License, which can be found at
//      http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx 
//
// @File: Configurator.cs
//
// Purpose: Reads configuration from a configuration file (by default - H2Config.xml)
//
//
//
// Notes: 
//
//
//	
// History: 
//     @Version: V2.1
//
//     07/14/08 PGV 2.1.001.0 Starting V2.1 (based on 2.0.030.0 code)
//
//
// @EndHeader@
//*********************************************************************

using System;
using System.Collections;
using System.Xml;

namespace H2V2
{
	public class ConfigException : Exception 
	{
		public ConfigException (string a_msg) : base (a_msg) {}
	}

    public class Computer
    {
        private string _Name;
        private string _ScanDrives = "";

        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }
        public string ScanDrives
        {
            get { return _ScanDrives; }
            set { _ScanDrives = value; }
        }
    }

	public class Configurator
	{
		public static ArrayList Computers_array;
		public static ArrayList Perf_array;

		public Configurator ()
		{
			Computers_array = new ArrayList ();
			Perf_array = new ArrayList ();
		}

		public void ReadConfig ()
		{
			XmlElement root;

			GetRoot (out root);
			ReadRepository (root);
			ReadSettings (root);
			ReadTargets (root);
			this.ReadPerfProviders (root);
		}

		void GetRoot (out XmlElement a_root)
		{
			XmlDocument xd = new XmlDocument ();
			xd.Load (CmdLineArg.ConfigFile);
			a_root = xd.DocumentElement;
		}

		void ReadRepository (XmlElement a_root)
		{
			string svalue;
			XmlNode xn;
			XmlNodeList tList;

			xn = a_root.SelectSingleNode("/ROOT/REPOSITORY"); 
			tList = xn.ChildNodes;

			foreach (XmlNode tnode in tList)
			{
				switch (tnode.NodeType)
				{
					case XmlNodeType.Element:
						svalue = tnode.SelectSingleNode("attribute::Value").Value;
					switch (tnode.Name)
					{
						case "SERVER": 
							Constants.RepositoryServer = svalue;
							break;
						case "DATABASE":
							Constants.RepositoryDB = svalue;
							break;
						default:
							throw new ConfigException (Constants.ConfigFileName+": Unknown node "+tnode.Name);
					}
						break;
					case XmlNodeType.Comment:
						continue;
					default:
						throw new ConfigException (Constants.ConfigFileName+": Unexpected node type "+tnode.NodeType);
				}
			}

		}

		void ReadSettings (XmlElement a_root)
		{
			string svalue;
			XmlNode xn;
			XmlNodeList tList;

			xn = a_root.SelectSingleNode("/ROOT/SETTINGS"); 
			tList = xn.ChildNodes;

			foreach (XmlNode tnode in tList)
			{
				switch (tnode.NodeType)
				{
					case XmlNodeType.Element:
						svalue = tnode.SelectSingleNode("attribute::Value").Value;
					switch (tnode.Name)
					{
						case "VerboseLogging":
							Constants.VerboseLogging = (svalue.ToLower () == "true"); 
							break;
						case "ExportData":
                            // v 2.1 - ignore this option
							break;
						case "DeleteFiles":
							Constants.DeleteFiles = (svalue.ToLower () == "true"); 
							break;
						case "RepositoryTimeout":
							try
							{
								int t;
								t = Convert.ToInt32 (svalue);
								Constants.RepositoryTimeout = t;
							}
							catch {}
							break;
						default:
							throw new ConfigException (Constants.ConfigFileName+": Unknown node "+tnode.Name);
					}
						break;
					case XmlNodeType.Comment:
						continue;
					default:
						throw new ConfigException (Constants.ConfigFileName+": Unexpected node type "+tnode.NodeType);
				}
			}
		}

		void ReadTargets (XmlElement a_root)
		{
			int idx;
			XmlNodeList tList;

			tList = a_root.SelectNodes("/ROOT/TARGET_LIST/TARGET"); 

			foreach (XmlNode tnode in tList)
			{
                Computer target_machine = new Computer();
                target_machine.Name = tnode.SelectSingleNode("attribute::Name").Value.ToUpper();
                if (tnode.SelectSingleNode("attribute::ScanDrives") != null)
                {
                    target_machine.ScanDrives = tnode.SelectSingleNode("attribute::ScanDrives").Value.ToUpper();
                }
                else
                {
                    target_machine.ScanDrives = "";
                }

                if (target_machine.Name == ".")
				{
                    target_machine.Name = Environment.MachineName.ToUpper();
				}

                idx = Computers_array.IndexOf(target_machine.Name);
				if (idx == -1) 
				{
                    Computers_array.Add(target_machine);
				}
			}

			Computers_array.TrimToSize ();
		}

		void ReadPerfProviders (XmlElement a_root)
		{
			int idx;
			string name;
			XmlNodeList tList;

			tList = a_root.SelectNodes("/ROOT/PERF_PROVIDERS/PERF_COLLECTOR"); 

			foreach (XmlNode tnode in tList)
			{
				name = tnode.SelectSingleNode("attribute::Name").Value.ToUpper ();
				if (name == ".")
				{
					name = Environment.MachineName.ToUpper ();
				}

				idx = Perf_array.IndexOf (name);
				if (idx == -1) 
				{
					Perf_array.Add (name);
				}
			}

			Perf_array.TrimToSize ();
		}
	}
}





		